package com.gec.it.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gec.it.entity.Depts;
import com.gec.it.entity.Doctors;
import com.gec.it.entity.Jobs;
import com.gec.it.service.DeptsService;
import com.gec.it.service.DoctorsService;
import com.gec.it.service.JobsService;
import com.gec.it.util.EmailUtil;
import com.gec.it.util.Md5Util;
import com.gec.it.util.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 欧阳
 * @since 2022-01-04
 */
@Controller
public class DoctorsController {

    @Autowired
    private DoctorsService service;

    @Autowired
    private DeptsService deptsService;

    @Autowired
    private JobsService jobsService;


    @GetMapping("/login")
    public String login(){
        return "login";
    }

    @PostMapping("/login")
    public String login(String uname, String pwd , HttpSession session, Model m){
        QueryWrapper<Doctors> qw = new QueryWrapper<>();
        qw.lambda().eq(Doctors::getName,uname).eq(Doctors::getPwd,Md5Util.digest(pwd+uname));
        final Doctors doc = service.getOne(qw);
        if (doc != null){
            session.setAttribute("doc",doc);
            return "redirect:index";
        }
        m.addAttribute("err","用户名或密码错误");
        return "login";
    }
    @GetMapping("/index")
    public String index(){
        return "index";
    }
    @GetMapping("/add")
    public String add(Model m){
        final List<Depts> depts = deptsService.list();
        final List<Jobs> jobs = jobsService.list();
        m.addAttribute("depts",depts);
        m.addAttribute("jobs",jobs);
        return "doc_add";
    }

    @Autowired
    private EmailUtil emailUtil;
    @Autowired
    private SmsUtil smsUtil;

    @PostMapping("/add")
    public String add(Doctors d, MultipartFile myfile, HttpServletRequest req) throws IOException {
        if(!myfile.isEmpty()) {
            //重命名图片
            String filename = myfile.getOriginalFilename();
            String ext=filename.substring(filename.lastIndexOf("."));//获取后缀.jpg .png
            String newFilename= UUID.randomUUID().toString()+ext;//3545-d.......png
            //定位保存路径
            String imgpath=req.getServletContext().getInitParameter("doc_imgs");//读配置
            String webapps=req.getServletContext().getRealPath("/");
            webapps=webapps.substring(0,webapps.lastIndexOf("webapps"))+"webapps/";
            
            //String webapps=new File(req.getServletContext().getRealPath("/")).getParent();//获得tomcat的webapps目录路径
            //自动创建文件夹
            File dir=new File(webapps+imgpath);
            if(!dir.exists())
                dir.mkdirs();
            //保存图片
            myfile.transferTo(new File(webapps+imgpath +"/"+ newFilename));
            d.setPhoto(newFilename);
        }
        System.out.println(d);
        d.setPwd(Md5Util.digest(d.getPwd()+d.getName()));
        //d.setCardNo(AESUtil.AESEncode("abc123",d.getCardNo));//工资卡号加密后再放到数据库
        service.save(d);
        emailUtil.sendMail("2184260057@qq.com","账户创建成功","尊敬的"+d.getName()+",你的系统账号已经创建，可以登录操作!欢迎加入团队");
        smsUtil.sendSms("137110309222","尊敬的"+d.getName()+",你的系统账号已经创建，可以登录操作!欢迎加入团队");
        //SmsUtil.sendSms("wfccpa","",d.getPhone(),"尊敬的"+d.getName()+",你的系统账号已经创建，可以登录操作!欢迎加入团队");
        return "redirect:list";
    }

    @GetMapping("/list")
    public String doctorList(Model m){
        final List<Doctors> doctors = service.list2();
        m.addAttribute("list",doctors);
        return "doc_list";
    }

    @GetMapping("/del")
    public String del(int id){
        service.removeById(id);
        return "redirect:list";
    }

    @GetMapping("/edit")
    public String edit(int id,Model m){
        final Doctors doctor = service.getById(id);
        final List<Depts> depts = deptsService.list();
        final List<Jobs> jobs = jobsService.list();
        m.addAttribute("depts",depts);
        m.addAttribute("jobs",jobs);
        m.addAttribute("d",doctor);
        return "doc_edit";
    }

    @PostMapping("/edit")
    public String edit(Doctors d, MultipartFile myfile) throws IOException {
        String filename = myfile.getOriginalFilename();
        myfile.transferTo(new File("D:\\HTML\\apache-tomcat-9.0.54\\webapps\\images\\album\\"+filename));
        d.setPhoto(myfile.getOriginalFilename());
        service.updateById(d);
        return "redirect:list";
    }
}

















